// pages/wode/wode.js
// pages/profile/profile.js
Page({
  data: {
    userInfo: {
      avatarUrl: '/images/default-avatar.png',
      nickName: '用户名'
    },
    checkInDays: 7,          // 打卡天数
    level: 1,                // 初始等级
    progress: 0,             // 进度条百分比
    dataItems: [
      {
        icon: '/images/icon-data.png',
        name: '我的数据',
        url: '/pages/stats/stats'
      },
      {
        icon: '/images/icon-trophy.png',
        name: '我的成就',
        url: '/pages/achievement/achievement'
      },
      {
        icon: '/images/icon-plan.png',
        name: '我的计划',
        url: '/pages/plan/plan'
      }
    ],
    settingItems: [
      {
        name: '设置',
        url: '/pages/settings/settings'
      },
      {
        name: '隐私管理',
        url: '/pages/privacy/privacy'
      },
      {
        name: '帮助',
        url: '/pages/help/help'
      },
      {
        name: '关于',
        url: '/pages/about/about'
      }
    ]
  },

  onLoad() {
    this.initUserData();
    this.calculateLevel();
  },

  initUserData() {
    const checkInDays = 7;
    this.setData({
      checkInDays: checkInDays
    }, () => {
      this.calculateLevel();
    });
  },

  calculateLevel() {
    const days = this.data.checkInDays;
    const level = Math.floor(days / 10) + 1;
    const progress = (days % 10) * 10;
    
    this.setData({
      level: level,
      progress: progress
    });
  },

  // 修改后的跳转方法（关键修改点）
  navigateTo(e) {
    const url = e.currentTarget.dataset.url;
    if (url) {
      // 先跳转到目标页面
      wx.navigateTo({
        url: url,
        success: () => {
          // 在目标页面的onUnload中设置reLaunch
          // 需要在每个子页面添加：
          // onUnload() { wx.reLaunch({ url: '/pages/home/home' }) }
        }
      });
    }
  },

  // 新增：页面卸载时强制返回首页
  onUnload() {
    wx.reLaunch({
      url: '/pages/zhuye/zhuye'
    });
  },

  getUserProfile() {
    wx.getUserProfile({
      desc: '用于展示用户信息',
      success: (res) => {
        this.setData({
          userInfo: res.userInfo
        });
        wx.setStorageSync('userInfo', res.userInfo);
      },
      fail: (err) => {
        console.error('获取用户信息失败', err);
      }
    });
  }
});